# -*- coding: utf-8 -*-

print "hello, world!"

with open('hzk24.c', 'r') as fl_std:
  std_lines = fl_std.readlines()

with open('SimSun_24.c', 'r') as fl_fix:
  fix_lines = fl_fix.readlines()
  
  #replace display area
  tn = 0
  for k in range(0, len(fix_lines)):
    if k >= len(fix_lines): break

    pos = fix_lines[k].find('acFontHZSimSun_24_')
    if pos == -1: continue
    if fix_lines[k].find('GUI_FLASH') != 0: continue
    
    fix_lines[k] = fix_lines[k].replace('[48]', '[]')

    pos += len('acFontHZSimSun_24_')
    code = fix_lines[k][pos : pos+4]
    searchStr = 'acFontHZ24_' + code

    for i in range(0, len(std_lines)):
      pos = std_lines[i].find(searchStr)
      if pos == -1: continue
      #find in stard file
      if std_lines[i].find('GUI_FLASH') == 0:
        idx = i+1
        while 1:
          if std_lines[idx].find('0x') == 0:
            valid = std_lines[idx]
            break
          idx += 1
        #replace
        idx = k+1
        while 1:
          if fix_lines[idx].find('}') == -1:
            del fix_lines[idx]
          else:
            break
        fix_lines.insert(idx, valid)
        tn += 1
        break

  #replace number
  for k in range(0, len(fix_lines)):
    if k >= len(fix_lines): break

    pos = fix_lines[k].find('acFontHZSimSun_24_')
    if pos == -1: continue
    if fix_lines[k].find('{') != 0: continue
    fix_lines[k] = fix_lines[k].replace(' 2,', ' 3,')
    fix_lines[k] = fix_lines[k].replace('25,', '24,')

with open('SimSun_24.c', 'w') as fl_res:
  buf = ''
  for ln in fix_lines:
    buf += ln
  fl_res.write(buf)

print('find %d lines' %(tn))

